package com.example.soa_exp2.Controller;

import com.example.soa_exp2.Config.CommonResponse;
import com.example.soa_exp2.Domain.Admin;
import com.example.soa_exp2.Service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@Tag(name = "操作员信息管理模块")
public class AdminController {
    @Resource
    public AdminService adminService;

    @Operation(summary = "新增操作员")
    @PostMapping
    public CommonResponse<Admin> addAdmin(@RequestBody Admin adm) {
        if(adm.getAccount()==null){
            return CommonResponse.BackForError("账号为空，新增操作员失败");
        }else if (adm.getPwd()==null) {
            return CommonResponse.BackForError("密码为空，新增操作员失败");
        }else{
            int result = adminService.addAdmin(adm);
            if(result == 0){
                return CommonResponse.BackForError("新增操作员失败");
            }else {
                return CommonResponse.BackForSuccess("新增操作员成功","http://127.0.0.1:8081/admin");
            }
        }
    }

    @Operation(summary = "修改操作员信息")
    @PutMapping
    public CommonResponse<Admin> updateAdmin(@RequestBody Admin adm){
        if(adm.getAccount()==null){
            return CommonResponse.BackForError("账号为空，修改操作员信息失败");
        } else if (adm.getPwd()==null) {
            return CommonResponse.BackForError("密码为空，修改操作员信息失败");
        } else{
            int result = adminService.updateAdmin(adm);
            if(result == 0){
                return CommonResponse.BackForError("修改操作员信息失败");
            }else {
                return CommonResponse.BackForSuccess("修改操作员信息成功","http://127.0.0.1:8081/admin");
            }
        }
    }

    @Operation(summary = "删除操作员信息")
    @DeleteMapping
    public CommonResponse<Admin> deleteAdminById(String admId){
        int result = adminService.deleteAdmin(admId);
        if(result == 0){
            return CommonResponse.BackForError("删除操作员信息失败");
        }else{
            return CommonResponse.BackForSuccess("删除操作员信息成功","http://127.0.0.1:8081/admin");
        }
    }

    @Operation(summary = "搜索操作员信息")
    @GetMapping
    public CommonResponse<List<Admin>> getAdminById(@RequestParam("account") String account){
        List<Admin> adm = adminService.getAdminByAccount(account);
        if (adm == null) {
            return CommonResponse.BackForError("未搜索到相关操作员信息");
        }else {
            return CommonResponse.BackForSuccess("搜索操作员信息成功", adm,"http://127.0.0.1:8081/admin");
        }
    }

}
